package com.aaa.controller;

import com.aaa.entity.Role;
import com.aaa.entity.Users;
import com.aaa.service.UserService;
import com.aaa.util.DefaultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UsersController {
    @Autowired
    private UserService usersService;

    @RequestMapping("/listData")
    public PageInfo listAll(String keyword, Integer currentPage, Integer pageSize){
        //调用PageHelper.startPage()
        PageHelper.startPage(currentPage,pageSize);
        //查询数据
        List<Users>list=usersService.listAll(keyword);
        //将数据返回前端
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }
    //    添加或者修改
    @RequestMapping("/addOrUpdate")
    public DefaultMsg addOrUpdate(Users users,String rolesId){

        return usersService.saveOrUpdate(users,rolesId);
    }
    //    删除
    @RequestMapping("/delete")
    public DefaultMsg delete(Long uid){
        return usersService.delete(uid);
    }
    //查询所有角色 复选框列表用
    @RequestMapping("/listAllRole")
    public List<Role> listAllRole(){
        return usersService.listAllRole();
    }

    //    查询当前用户拥有几个角色
    @RequestMapping("/getUserRolesCountByUid")
    public Integer getUserRolesCountByUid(Long uid){
        return usersService.getUserRolesCountByUid(uid);
    }
    //    delRole
    @RequestMapping("/delRole")
    public Integer delRole(Long uid,Long rid){
        return usersService.delRole(uid,rid);
    }
    //    重置密码
    @RequestMapping("/resetPassword")
    public Integer resetPassword(Long uid){
        return usersService.resetPassword(uid);
    }
}
